////////////////////////////////////////////////////////////////
///
/// File: TmpExp.m
/// Author: Lucio Davide Spano
/// Last update: 17 August 2012
/// Description: This file contains the basic class for
///              building temporal gesture expressions
/// To do:
/// ------------------------------------------------------------
/// Copyright (c) 2012 Lucio Davide Spano
///                     (lucio.davide.spano@isti.cnr.it)
///
/// The use and distribution terms for this software are
/// contained in the file named license.txt, which can be found
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------
////////////////////////////////////////////////////////////////

#import "TmpExp.h"
#import "TmpExpProtected.h"
#import "StateChangeHandler.h"
#import "GestureListener.h"


@implementation TmpExp


@synthesize state;
@synthesize parent;
@synthesize iterative;
@synthesize optional;
@synthesize type;

- (id) init {
    self = [super init];
    if(self){
        self.state = Disabled;
        self.parent = nil;
        self.iterative = false;
        self.optional = false;
        self.type = Simple;
        
        self->stateChangedListeners = [[NSMutableArray alloc] init];
        self->gestureListeners = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void) setExecState:(int) vState : (Token*) vToken{
    self.state = vState;
}

- (void) raiseOnStateChanged :(TmpExp*) sender :(ExpEventArgs*) args :(Token*) _token{
    if(self->stateChangedListeners != nil){
        for(id<StateChangeHandler> h in self->stateChangedListeners){
            [h onStateChanged:sender :args :_token];
        }
    }
}
- (void) raiseCompleted :(TmpExp*) sender :(ExpEventArgs*) args :(Token*) _token{
    if(self->gestureListeners != nil){
        for(GestureListener* h in self->gestureListeners){
            [h completed:sender :args :_token];
        }
    }
    
}
- (void) raisePerforming :(TmpExp*) sender :(ExpEventArgs*) args :(Token*) _token{
    if(self->gestureListeners != nil){
        for(GestureListener* h in self->gestureListeners){
            [h performing:sender :args :_token];
        }
    }
    
}
- (void) raiseError :(TmpExp*) sender :(ExpEventArgs*) args :(Token*) _token{
    if(self->gestureListeners != nil){
        for(GestureListener* h in self->gestureListeners){
            [h error:sender :args :_token];
        }
    }
}

- (void) addGestureListener:(GestureListener*)listener{
    [self->gestureListeners addObject:listener];
}

- (void) removeGestureListener:(GestureListener*)listener{
    [self->gestureListeners removeObject:listener];
}

- (void) addStateChangeListener:(id<StateChangeHandler>)listener{
    [self->stateChangedListeners addObject:listener];
}

- (void) removeStateChangeListener:(id<StateChangeHandler>)listener{
    [self->stateChangedListeners removeObject:listener];
}

- (void) raiseOnNotAcceptedToken :(SimpleTmpExp*) originalSender :(ExpEventArgs*) args :(Token*) _token{
    if(self->stateChangedListeners != nil){
        for(id<StateChangeHandler> h in self->stateChangedListeners){
            [h onNotAcceptedToken :self :originalSender :args :_token];
        }
    }
}

- (bool) accepts:(ExpEventArgs *)args{
    return false;
}
@end
